using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace TestResizeCrop
{
  public partial class frmResizeCrop : Form
  {
    private Bitmap img;

    public frmResizeCrop()
    {
      InitializeComponent();
    
    }

      // this is where the scaling of the mouse crousor is adjsuting

      private void HandleMouseMove(object sender, MouseEventArgs e)            
      {                
          // Scale the window coordinates to the bitmap coordinates                
          double windowX = e.X;                
          double windowY = e.Y;                
          double controlWidth = pbImage.Width;                
          double controlHeight = pbImage.Height;
          double imageWidth = img.Width;
          double imageHeight = img.Height;
          int X=(int)(windowX * img.Width / controlWidth);
          int Y=(int)(windowY * img.Height / controlHeight);
          Color pixel = img.GetPixel(X,Y);
          updateLocClrInfo(pixel.ToString(), (new Point(X, Y)).ToString());
          //System.Diagnostics.Debug.WriteLine(pixel);            
      }


      // open image handler
    private void btnOpenImage_Click(object sender, EventArgs e)
    {
      OpenFileDialog openFileDia = new OpenFileDialog();
      if (openFileDia.ShowDialog() == DialogResult.OK)
      {
        this.img = new Bitmap(openFileDia.FileName);
        this.setPicBoxImage();
        this.butSave.Enabled = true;
        this.ZooCenbtn.Enabled = true;
        pbImage.MouseMove += new MouseEventHandler(HandleMouseMove);
      }
    }

    // setting the image in image box and center/zoom it 
    private void setPicBoxImage()
    {
      this.pbImage.Image = img;
      if (img.Height < pbImage.Height && img.Width < pbImage.Width)
      {
        this.pbImage.SizeMode = PictureBoxSizeMode.CenterImage;
        this.ZooCenbtn.Text = "Zoom";
      }
      else
      {
        this.ZooCenbtn.Text = "Center";
      }
      this.updateImgInfo();
    }

      // updating image size in the frame
    private void updateImgInfo()
    {
      this.lblImgHeightAct.Text = this.img.Height.ToString();
      this.lblImgWidthAct.Text = this.img.Width.ToString();
    }

    private void updateLocClrInfo(string c,string p)
    {
        this.lblClrValue.Text = c;
        this.lblLocValue.Text = p;
    }
      // save file handler

    private void butSave_Click(object sender, EventArgs e)
    {
        int[][] matrix= new int[img.Width][];
        for (int i = 0; i < img.Width; i++)
        {
            matrix[i] = new int[img.Height];
        }
      SaveFileDialog saveFileDia = new SaveFileDialog();
      saveFileDia.Filter = "txt files (*.txt)|*.txt";
      if (saveFileDia.ShowDialog() == DialogResult.OK)
      {
          for (int x = 0; x < img.Width; x++)
          {
              for (int y = 0; y < img.Height; y++)
              {
                  matrix[x][y] = 255*3-((int)img.GetPixel(x, y).R + (int)img.GetPixel(x, y).G + (int)img.GetPixel(x, y).B);
              }
          }
          
          Desk D = new Desk(matrix);
          D.Scan();
          D.toTextFile(saveFileDia.FileName);

        //this.saveJpeg(saveFileDia.FileName, new Bitmap(this.img), 85L);
      }
    }

      // zoom / center handler
    private void ZooCenbtn_Click(object sender, EventArgs e)
    {

        if (this.pbImage.SizeMode == PictureBoxSizeMode.CenterImage)
        {
            this.pbImage.SizeMode = PictureBoxSizeMode.Zoom;
            this.ZooCenbtn.Text = "Center";
            return;
        }
        else
        {
            this.pbImage.SizeMode = PictureBoxSizeMode.CenterImage;
            this.ZooCenbtn.Text = "Zoom";
        }
    }


    /*
     * Image editing code below 
     * 
     * 
     */

    

    private void saveJpeg(string path, Bitmap img, long quality)
    {
      // Encoder parameter for image quality
      EncoderParameter qualityParam =
          new EncoderParameter(Encoder.Quality, quality);

      // Jpeg image codec
      ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

      if (jpegCodec == null)
        return;

      EncoderParameters encoderParams = new EncoderParameters(1);
      encoderParams.Param[0] = qualityParam;

      img.Save(path, jpegCodec, encoderParams);
    }

    private ImageCodecInfo getEncoderInfo(string mimeType)
    {
      // Get image codecs for all image formats
      ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

      // Find the correct image codec
      for (int i = 0; i < codecs.Length; i++)
        if (codecs[i].MimeType == mimeType)
          return codecs[i];
      return null;
    }


    

  }
}